package client.comm.commlib.dialog

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import client.comm.commlib.R
import client.comm.commlib.comm_ui.bean.Areas

import client.comm.commlib.databinding.DialogSelectaddrLayoutBinding
import client.comm.commlib.utils.listFromPath
import client.tzstore.oo.dialog.BottomDialog

import client.tzstore.oo.dialog.adapter.SelectShengAdapter

import java.io.InputStream

class SelectAddrDialog(context: Context) : BottomDialog(context) {

    val shengAdapter: SelectShengAdapter by lazy {
        SelectShengAdapter(context).apply {
            event = {
                binding.twoTab.performClick()
                binding.twoTab.visibility = View.VISIBLE
                binding.threeTab.visibility = View.GONE
                binding.shengTv.text = it.name
                binding.shiTv.text = "请选择"
                shiAdapter.select = -1
                quAdapter.select = -1
            }
        }
    }
    val shiAdapter: SelectShengAdapter by lazy {
        SelectShengAdapter(context).apply {
            event = {
                binding.threeTab.visibility = View.VISIBLE
                binding.threeTab.performClick()
                binding.shiTv.text = it.name
                binding.quTv.text = "请选择"
                quAdapter.select = -1
            }
        }
    }
    val quAdapter: SelectShengAdapter by lazy {
        SelectShengAdapter(context).apply {

            event = { city ->
                ondismis?.let {
                    val one = shengAdapter.getItems()
                        .get(shengAdapter.select)
                    val two = shiAdapter.getItems()
                        .get(shiAdapter.select)
                    it(one!!, two!!, city)
                }
                dismiss()
            }
        }
    }

    val citys: Areas by lazy {
        val input: InputStream = context.resources.assets.open("c_areas.json")
        val citys = input.listFromPath<Areas>()
        citys!!
    }
    val binding by lazy {
        DataBindingUtil.inflate<DialogSelectaddrLayoutBinding>(
            LayoutInflater.from(context),
            R.layout.dialog_selectaddr_layout,
            null,
            false
        )
    }


    override fun getV(): View {
        binding.event = this
        binding.recyclerView.adapter = shengAdapter
        binding.recyclerView2.adapter = shiAdapter
        binding.recyclerView3.adapter = quAdapter
        return binding.root
    }

    fun showDialog(one: String, two: String, three: String) {
        val twoList = ArrayList<Areas.City>()
        for (v in citys.RECORDS) {
            if (v.parent_id.isNullOrEmpty()) {
                twoList.add(v)
            }
        }
        shengAdapter.submitList(twoList)

        if (!TextUtils.isEmpty(one)) {
            binding.shengTv.text = one
            binding.oneTab.visibility = View.VISIBLE
            shengAdapter.setSelect(one)
            val twoList = ArrayList<Areas.City>()

            for (v in citys.RECORDS) {
                if (v.parent_id.equals(shengAdapter.selectId)) {
                    twoList.add(v)
                }
            }
            shiAdapter.submitList(twoList)
            if (TextUtils.isEmpty(two))
                binding.recyclerView.adapter = shengAdapter
        } else {
            binding.oneTab.isSelected = true
            binding.selectTag = 0
        }
        if (!TextUtils.isEmpty(two)) {
            binding.shiTv.text = two
            binding.twoTab.visibility = View.VISIBLE
            binding.twoTab.isSelected = true

            binding.oneTab.isSelected = false
            shiAdapter.setSelect(two)
            val twoList = ArrayList<Areas.City>()

            for (v in citys.RECORDS) {
                if (v.parent_id.equals(shiAdapter.selectId)) {
                    twoList.add(v)
                }
            }
            quAdapter.submitList(twoList)
            if (TextUtils.isEmpty(three)) {
                binding.selectTag = 1
            }
        } else {
            binding.twoTab.visibility = View.GONE
        }
        if (!TextUtils.isEmpty(three)) {
            binding.quTv.text = three
            binding.threeTab.visibility = View.VISIBLE
            binding.threeTab.isSelected = true
            binding.oneTab.isSelected = false
            binding.twoTab.isSelected = false

            quAdapter.setSelect(three)
            binding.selectTag = 2
        } else {
            binding.threeTab.visibility = View.GONE
        }


        show()
    }

    fun tabClick(view: View) {
        val tag = view.tag
        if ("${binding.selectTag}".equals(tag))
            return
        when (tag) {
            "0" -> {
                binding.selectTag = 0

                view.isSelected = true
                binding.twoTab.isSelected = false
                binding.threeTab.isSelected = false
            }
            "1" -> {
                binding.selectTag = 1

                val twoList = ArrayList<Areas.City>()

                for (v in citys.RECORDS) {
                    if (v.parent_id.equals(shengAdapter.selectId)) {
                        twoList.add(v)
                    }
                }

                shiAdapter.submitList(
                    twoList
                )
                view.isSelected = true
                binding.oneTab.isSelected = false
                binding.threeTab.isSelected = false
            }
            "2" -> {
                binding.selectTag = 2
                val twoList = ArrayList<Areas.City>()

                for (v in citys.RECORDS) {
                    if (v.parent_id.equals(shiAdapter.selectId)) {
                        twoList.add(v)
                    }
                }
                quAdapter.submitList(twoList)
                view.isSelected = true
                binding.twoTab.isSelected = false
                binding.oneTab.isSelected = false
            }
        }
    }

    private var ondismis: ((one: Areas.City, two: Areas.City, three: Areas.City) -> Unit)? = null

    fun onDismis(ondis: ((one: Areas.City, two: Areas.City, three: Areas.City) -> Unit)?) {
        this.ondismis = ondis
    }


}